package com.sduept.nwld.dataserver.controller.distribute;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.ledger.enums.ManagementUnitEnum;
import com.sduept.cim.ledger.manager.SubstationLedgerManager;
import com.sduept.cim.ledger.model.SubstationLedger;
import com.sduept.cim.model.core.Substation;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.manager.distribute.PersonAndEquipmentDistributeManger;
import com.sduept.nwld.dataserver.model.distribute.PersonAndEquipmentDistribute;
import com.sduept.permission.entity.DepartmentEntityBean;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.DepartManager;
import com.sduept.utils.PingYinUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Named
@ViewScoped
public class StationDistributeController extends AbstractController {
	private static final long serialVersionUID = -6515793515265256769L;
	
	@Autowired
	private PersonAndEquipmentDistributeManger distributeM;
	@Autowired
	private SubstationLedgerManager stationLedgerM;
	@Autowired
	private CimManager cimM;
	@Autowired
	private DepartManager departM;
	
	private List<PersonAndEquipmentDistribute> distributes = new ArrayList<PersonAndEquipmentDistribute>();
	private List<PersonAndEquipmentDistribute> selectDistributes = new ArrayList<PersonAndEquipmentDistribute>();
	
	private List<Substation> subStations = new ArrayList<Substation>();
	private List<Substation> selectSubStations = new ArrayList<Substation>();
	
	private String vol = "500"; //电压等级默认为500
	private String status = "0"; //设备状态
	private String stationName = "";
	
	private List<String> standVoltages = new ArrayList<String>();
	private Map<String,Substation> map = new HashMap<String, Substation>();
	
	@PostConstruct
	public void init() {
		getStandVols();
		distributes = distributeM.findEquipmentDistribute(vol, status, stationName, getCurrentUser());
		map = getStationMap();
	}

	public SubstationLedger getStationLedger(PersonAndEquipmentDistribute distribute) {
		String stationId = distribute.getEquipmentId();
		SubstationLedger ledger = stationLedgerM.findSubstationLedger(stationId);
		return ledger;
	}
	
	public Substation getStation(PersonAndEquipmentDistribute distribute) {
		String stationId = distribute.getEquipmentId();
		Substation station = cimM.getStationById(stationId);
		return station;
	}
	
	public List<PersonAndEquipmentDistribute> getStationDistribute(PersonAndEquipmentDistribute distribute) {
		String stationId = distribute.getEquipmentId();
		List<PersonAndEquipmentDistribute> list = distributeM.getEquipmentDistributeByStationId(stationId);
		return list;
	}
	
	public void deleteDistributes() {
		if(selectDistributes.size()>0) {
			for(PersonAndEquipmentDistribute dist : selectDistributes) {
				distributeM.delete(dist);
				distributes.remove(dist);
			}
			selectDistributes.clear();
			addSuccessMessage("移除成功！");
		}
	}
	
	public void query() {
		distributes = distributeM.findEquipmentDistribute(vol, status, stationName, getCurrentUser());
	}
	
	public void initQueryCondition() {
		vol = "500"; 
		status = "0"; 
		stationName = "";
		selectSubStations.clear();
		queryStation();
	}
	
	private Map<String, Substation> getStationMap() {
		Map<String,Substation> map = new HashMap<String, Substation>();
		List<Substation> stationList = cimM.findUsedAndUnUsedStations();
		for (Substation station : stationList) {
			map.put(station.getId(), station);
		}
		return map;
	}
	/**
	 * 获得当前用户的所属部门，找到该部门以及该部门的子部门下的管辖变电站
	 */
	public void queryStation() {
		subStations.clear();
		PersonnelEntityBean p = getCurrentUser();
		if("admin".equals(p.getCode())) {
			// 如果当前用户为管理员，展示所有的变电站
			subStations = cimM.findUsedAndUnUsedStations();
		} else {
			DepartmentEntityBean dept = getCurrentUser().getGroup();
			// 如果当前部门的部门类型为空（还未设置），默认为其类型为调管单位
			if(null != dept) {
				String deptType = dept.getType();
				if(StringUtils.isEmpty(deptType)) {
					deptType = ManagementUnitEnum.MAINTENANCE_SCOPE.name();
				}
				List<DepartmentEntityBean> deptList = departM.recursiveFindSubDepartments(dept);
				deptList.add(dept);
				for (DepartmentEntityBean dBean : deptList) {
					List<SubstationLedger> stationLedger = new ArrayList<SubstationLedger>();
					if(deptType.equals(ManagementUnitEnum.DISPATCHING_MANAGEMENT.name())) {
						// 调管单位
						stationLedger = stationLedgerM.queryStationLedgerByCondition(vol, stationName, dBean.getName(), null, null, null, null, null, null, null);
					} else if (deptType.equals(ManagementUnitEnum.MAINTENANCE_SCOPE.name())) {
						// 运维单位
						stationLedger = stationLedgerM.queryStationLedgerByCondition(vol, stationName, null, null, null, null, null, dBean.getName(), null, null);
					} else if (deptType.equals(ManagementUnitEnum.MAINTENANCE_MANAGEMENT.name())) {
						// 运维单位
						stationLedger = stationLedgerM.queryStationLedgerByCondition(vol, stationName, null, dBean.getName(), null, null, null, null, null, null);
					} else if (deptType.equals(ManagementUnitEnum.POWER_GENERATION_ENTERPRISES.name())) {
						// 运维单位
						stationLedger = stationLedgerM.queryStationLedgerByCondition(vol, stationName, null, dBean.getName(), null, null, null, null, null, null);
					}
					for (SubstationLedger ledger : stationLedger) {
						Substation s = map.get(ledger.getCimId());
						subStations.add(s);
					}
				}
			}
		}
	}
	
	public void addStation() {
		for(Substation station : selectSubStations) {
			List<PersonnelEntityBean> govers = distributeM.getGovernorBySubstationId(station.getId());
			if(!govers.contains(getCurrentUser())) {
				PersonAndEquipmentDistribute distribute = new PersonAndEquipmentDistribute();
				distribute.setEquipmentId(station.getId());
				distribute.setEquipmentName(station.getName());
				distribute.setPersonId(getCurrentUser().getId());
				distribute = distributeM.createOrUpdate(distribute);
			}
		}
		addSuccessMessage("管理变电站成功！");
		selectSubStations.clear();
		query();
	}
	
	/**
	 * 获取标准电压等级
	 */
	private void getStandVols() {
		List<Float> vols = cimM.findStationStandVolLevels();
		for (Float vol : vols) {
			String volStr = String.valueOf(vol);
			volStr = volStr.replace(".0", "");
			standVoltages.add(volStr);
		}
	}
	
	public List<String> complateStation(String query){
		List<Substation> allStations = cimM.findUsedAndUnUsedStations();
		List<String>names = new ArrayList<String>();
		if (Pattern.matches("[a-z]{1,}", query)) {
			for (Substation station : allStations) {
				String nameHead = PingYinUtil.getPingYinHeader(station.getName());
				if (nameHead.toLowerCase().contains(query)) {
					names.add(station.getName());
				}
			}
		}else{
			for (Substation station : allStations) {
				if (station.getName().contains(query)) {
					names.add(station.getName());
				}
			}
		}
		return names;
	}
	public List<PersonAndEquipmentDistribute> getDistributes() {
		return distributes;
	}

	public void setDistributes(List<PersonAndEquipmentDistribute> distributes) {
		this.distributes = distributes;
	}

	public List<PersonAndEquipmentDistribute> getSelectDistributes() {
		return selectDistributes;
	}

	public void setSelectDistributes(
			List<PersonAndEquipmentDistribute> selectDistributes) {
		this.selectDistributes = selectDistributes;
	}

	public String getVol() {
		return vol;
	}

	public void setVol(String vol) {
		this.vol = vol;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<String> getStandVoltages() {
		return standVoltages;
	}

	public void setStandVoltages(List<String> standVoltages) {
		this.standVoltages = standVoltages;
	}

	public List<Substation> getSubStations() {
		return subStations;
	}

	public void setSubStations(List<Substation> subStations) {
		this.subStations = subStations;
	}

	public List<Substation> getSelectSubStations() {
		return selectSubStations;
	}

	public void setSelectSubStations(List<Substation> selectSubStations) {
		this.selectSubStations = selectSubStations;
	}

	public String getStationName() {
		return stationName;
	}

	public void setStationName(String stationName) {
		this.stationName = stationName;
	}
	
	
}
